<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>标题</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css">
    <link rel="stylesheet" href="/static/css/weadmin.css">
    <link rel="stylesheet" href="/lib/jqueryZtree/3.5.12/css/zTreeStyle/zTreeStyle.min.css">
    <script type="text/javascript" src="/lib/jquery/jquery.js" charset="utf-8"></script>
    <script type="text/javascript" src="/lib/jqueryZtree/3.5.12/js/jquery.ztree.all-3.5.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<!--
图纸目录树
-->
<div class="layui-row">
    <div class="layui-col-xs4">
        <div id="drawingDirTree" class="ztree"></div>
    </div>
    <div class="layui-col-xs8">
        <table class="layui-hide" id="dataList" lay-filter="dataList"></table>
    </div>
</div>



<script type="text/html" id="toolbarOrg">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="uploadDrawing">上传图纸</button>
        <button class="layui-btn layui-btn-sm" lay-event="removeDrawing">批量删除</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="show">查看</a>
</script>

<script type="text/javascript" src="/lib/layui/layui.js"></script>
<script src="/static/js/main.js"></script>
<script>
    layui.use(['jquery', 'configureCom', 'configure', 'sessionFun', 'table','ztree','matchType'], function () {
        var $ = layui.jquery;
        var configure = layui.configure;
        var table = layui.table;
        var sessionFun = layui.sessionFun;
        var ztree = layui.ztree;
        var matchType = layui.matchType;
        var configureCom = layui.configureCom;
        var areaId = configureCom.GetQueryString('areaId');

        var cols = [[
            {type: 'checkbox'},
            {title: '序号', type: 'numbers'},
            {field: 'name', width:350, title: '图纸名称'},
            {fixed: 'right', title: '操作',width:150, toolbar: '#barDemo', width: 120}
        ]];


        configureCom.tableRenderGet(configure.PRJ_WORK_AREA_DRAWING_GET_API, {areaId: areaId}, cols, 'dataList', 10, 'full-100', 'toolbarOrg', false);
        // configureCom.tableSearch();
        // configureCom.selectFun('USE_STATUS', '', '#useStatus',configure.SEARCH_DICT_HTTP);


        //头工具栏事件
        table.on('toolbar(dataList)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            sessionFun.remove(configure.SESSION_LIST_DATA);
            switch (obj.event) {
                case 'uploadDrawing':
                    if(!selectDirId){
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }

                    sessionFun.set(configure.SESSION_UPLOAD_CONFIG,{
                        url:configure.PRJ_WORK_AREA_DRAWING_UPLOAD_API,
                        formData:{
                            areaId:areaId,
                            docId:docId,
                            dirId:selectDirId
                        }
                    });
                    top.layer.open({
                        type: 2,
                        title: '上传图纸',
                        content: configure.UPLOAD_HTML,
                        area: configureCom.Psize1,
                        cancel: function (index, layero) {
                            //fileInit(fileTreeData.id);
                            configureCom.tableReload();
                        }
                    });
                    break;
                case 'removeDrawing':
                    var data = checkStatus.data;
                    if (!data.length) {
                        top.layer.alert('请您至少选择一条数据后再提交！');
                        return false;
                    }
                    top.layer.confirm('您真的要删除选中的数据吗？', function (index, layero) {
                        top.layer.close(index);
                        var ids = '';
                        $(data).each(function (index, item) {
                            if (!index) {
                                ids = item.id;
                            } else {
                                ids += ',' + item.id;
                            }
                        });
                        deleteFun(ids);
                    });
                    break;
            }
        });


        /**
         * 每行操作
         */
        table.on('tool(dataList)', function (obj) {
            var data = obj.data;
            sessionFun.set(configure.SESSION_DRAWING_DATA, data);
            // 查看图纸
            alert(matchType)
            if (obj.event === 'show') {
                top.layer.open({
                    type: 2,
                    title: '查看图纸',
                    area: configureCom.Psize1,
                    content: configure.CHECK_DRAWING_DRAWING_DRAWING_READ_HTML+'?fileId='+data.id+'&fileType='+matchType.init(data.name),
                    success: function (layero, index) {
                        top.layer.full(index);
                    }
                });
            }
        });


        /**
         * 图纸目录树状结构显示
         */
        var selectDirId = null;
        var docId = null
        function deptZtreeFun() {
            //ztree初始化
            configureCom.getAjax(configure.PRJ_WORK_AREA_DRAWING_DIR_TREE_API,{areaId:areaId}).then(function (res) {
                if(res.result!=null && res.result.length >0 ){
                    docId = res.result[0].docId;
                }
                ztree.param.multiCheck = false; //是否多选
                ztree.param.idKey = 'id';           //父级id
                ztree.param.pIdKey = 'parentId';    //子级中与父级id关联
                ztree.param.rootPId = null;         //根节点id
                ztree.param.name = 'name';
                ztree.param.onClick = function(event, treeId, treeNode){
                    selectDirId = treeNode.id;
                    configureCom.tableReload(false,{dirId:selectDirId});
                }
                ztree.init('drawingDirTree', '', res.result);
            });
        };
        deptZtreeFun();



        /**
         * 批量删除图纸
         * @param {Object} ids
         */
        function deleteFun(ids) {
            top.layer.confirm("您真的要删除这条数据吗？", function (index, layero) {
                configureCom.postDataAjax(configure.PRJ_WORK_AREA_DRAWING_DEL_API, {ids: ids}).then(function (res) {
                    if (res.code == 0) {
                        top.layer.msg('删除成功！');
                        configureCom.tableReload(true);
                    } else {
                        top.layer.alert('删除失败！');
                    }
                });
            });
        }

        /**
         * @param {Object} title
         * @param {Object} url
         */
        function editItem(title, url) {
            // top.layer.open({
            //     type: 2,
            //     title: title,
            //     area: configureCom.Psize1,
            //     content: configureCom.nocache(configure.ROLE_DETAIL_HTML),
            //     btn: ['保存'],
            //     yes: function (index, layero) {
            //         var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对
            //         var data = iframeWin.getData();
            //         data.deptId = selectDeptId;
            //         console.log(data);
            //         configureCom.postDataAjax(url, data).then(function (res) {
            //             if (res.code == 0) {
            //                 top.layer.close(index);
            //                 top.layer.msg('保存成功！');
            //                 configureCom.tableReload(true);
            //             } else {
            //                 top.layer.alert('保存失败！');
            //             }
            //         });
            //     }
            // });
        }

    });

</script>
</body>
</html>